<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Http\Controller\Test;

use App\Common\MyBean;
use App\Model\Logic\RequestBean;
use App\Model\Logic\RequestBeanTwo;
use Swoft\Bean\BeanFactory;
use Swoft\Co;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;

/**
 * Class SwoftBeanController
 *
 * @since 2.0
 *
 * @Controller(prefix="/bean")
 */
class SwoftBeanController
{
    /**
     * @RequestMapping(route="single",method={RequestMethod::GET})
     *
     * @return array
     */
    public function singleton()
    {
        $b = BeanFactory::getBean(MyBean::class);

        return [$b->myMethod()];
    }

    /**
     * @RequestMapping(route="req",method={RequestMethod::GET})
     *
     * @return array
     */
    public function request()
    {
        $id = (string)Co::tid();

        /** @var RequestBean $request */
        $request = BeanFactory::getRequestBean('requestBean', $id);

        $request->temp = ['rid' => $id];

        return $request->getData();
    }

    /**
     * @RequestMapping(route="req2",method={RequestMethod::GET})
     * @return array
     */
    public function requestTwo()
    {
        $id = (string)Co::tid();

        /* @var RequestBeanTwo $request */
        $request = BeanFactory::getRequestBean(RequestBeanTwo::class, $id);
        return $request->getData();
    }
}
